package com.caigou.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jiliang.purchase.demo.BusinessProcessing;
import jiliang.purchase.demo.CriteriaYongHulieBiao;
import jiliang.purchase.demo.YongHuliebiao;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import sch.jl.caigou.utils.Page;

import com.caigou.service.YongHuliebiaoService;

public class YongHuExportServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	private YongHuliebiaoService YHS = new YongHuliebiaoService();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel;charset=utf-8");

		String filedisplay = "yonghuliebiao.xls";
		response.addHeader("Content-Disposition","attachment;filename="+ filedisplay);
		
		String xingming = null;
		String dw_id = null;
		String js_id = null;
		int pageNum = 1;		
		String returnTitle = "";
		
		if(request.getParameter("pageNum") != null && !request.getParameter("pageNum").equals(""))
		{
			pageNum = Integer.valueOf(request.getParameter("pageNum"));
		}
		
		if(request.getParameter("xingming")!=null && !request.getParameter("xingming").equals(""))
		{
			xingming = request.getParameter("xingming");
			xingming = java.net.URLDecoder.decode(xingming, "UTF-8");
			returnTitle+=xingming+"|";
		}
		else
		{
			returnTitle+="a"+"|";
		}
		
		if(request.getParameter("dwid")!=null && !request.getParameter("dwid").equals("0"))
		{
			dw_id = request.getParameter("dwid");
			returnTitle+=dw_id+"|";
		}
		else
		{
			returnTitle+="b"+"|";
		}
		
		if(request.getParameter("jsid")!=null && !request.getParameter("jsid").equals("0"))
		{
			js_id = request.getParameter("jsid");
			returnTitle+=js_id+"|";
		}
		else
		{
			returnTitle+="c";
		}
		//System.out.println(returnTitle);
		CriteriaYongHulieBiao CYH = new CriteriaYongHulieBiao(xingming,"4",js_id,pageNum);
		
		Page<YongHuliebiao> page = new Page<YongHuliebiao>();
		page = YHS.getPage(CYH);
		List<YongHuliebiao> list = new ArrayList<YongHuliebiao>();
		list = YHS.getList(CYH);
		
		try {
			writeExcel(response.getOutputStream(),list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//request.setAttribute("page",page);
		//request.setAttribute("returnTitle", returnTitle);
		
	//	request.getRequestDispatcher("leibiao.jsp").forward(request, response);
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
	
	public void writeExcel(OutputStream out,List<YongHuliebiao> list)throws Exception
	{
		WritableWorkbook book = Workbook.createWorkbook(out);
		WritableSheet sheet = book.createSheet("sheet1", 0);
		
		String[] title={"工号","姓名","角色"};
		
		for(int i=0;i<title.length;i++)
		{
			Label label = new Label(i,0,title[i]);
			sheet.addCell(label);
		}
		
		for(int i=1;i<list.size();i++)//行
		{	
				Label label1 = new Label(0,i,list.get(i).getGonghao());
				//jxl.write.Number number = new jxl.write.Number(i,0,Double.valueOf(list.get(i).getBh())); 
				sheet.addCell(label1);
				Label label = new Label(1,i,list.get(i).getXingming());
				sheet.addCell(label);
				label = new Label(3,i,list.get(i).getJs_id());
				sheet.addCell(label);
		}		
			book.write();
			book.close();
	}

}
